import unittest
import requests
import jsonpath
import json
from common.config_utils import config

class TestGetAccesstoken(unittest.TestCase):
    def setUp(self) -> None:
        self.host=config.Host
        self.seesion=requests.session()
    def tearDown(self) -> None:
        self.seesion.close()
    def test_login_01(self):
        self._testMethodName='accestoken01'
        self._testMethodDoc='测试获取token正常'
        url_param = {"grant_type": "client_credential",
                     "appid": "wx71ad615ece9fc889",
                     "secret": "0ea4dbdbc5237ce7fe83def223d80ff2"}
        rp = self.seesion.get('https://%s/cgi-bin/token'%self.host,
                                params=url_param)
        rp_token_j = json.loads(rp.content.decode('utf-8'))
        expires_in = jsonpath.jsonpath(rp_token_j,'$.expires_in')[0]
        self.assertEqual(expires_in,7200)

    def test_login_02(self):
        self._testMethodName='grant_type为空'
        self._testMethodDoc='验证grant_type为空'
        url_param = {"grant_type": "",
                     "appid": "wx71ad615ece9fc889",
                     "secret": "0ea4dbdbc5237ce7fe83def223d80ff2"}
        rp = self.seesion.get('https://%s/cgi-bin/token'%self.host,
                              params=url_param)
        rp_token_j = rp.json()
        expires_in = jsonpath.jsonpath(rp_token_j,'$.errcode')[0]
        print(expires_in)
        self.assertEqual(expires_in, 40002)



# if __name__=='__main__':
#     unittest.main(verbosity=2)




